package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.hotel.saas.core.dao.StoreMarketingDAO;
import com.ecoolex.hotel.saas.entity.StoreMarketing;
import com.ecoolex.hotel.saas.entity.StoreMarketingExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.List;

@Service
public class StoreMarketingManager {

    @Autowired
    private StoreMarketingDAO dao;

    public StoreMarketing get(Integer id) {
        return dao.selectByPrimaryKeyWithLogicalDelete(id, StoreMarketing.NOT_DELETED);
    }

    public Pagination<StoreMarketing> query(String name, Integer storeId, Page page) {
        StoreMarketingExample example = new StoreMarketingExample();
        StoreMarketingExample.Criteria criteria = example.createCriteria();
        if (Check.notNull(name)) {
            criteria.andNameLike("%"+name+"%");
        }
        criteria.andStoreIdEqualTo(storeId).andDeletedEqualTo(StoreMarketing.NOT_DELETED);
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());
        example.orderBy(StoreMarketing.Column.weight.desc());
        return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
    }

    public List<StoreMarketing> query(Integer storeId, Integer size) {
        StoreMarketingExample example = new StoreMarketingExample();
        StoreMarketingExample.Criteria criteria = example.createCriteria();
        criteria.andStoreIdEqualTo(storeId).andDeletedEqualTo(StoreMarketing.NOT_DELETED);
        example.orderBy(StoreMarketing.Column.weight.desc());
        example.limit(size);
        return dao.selectByExample(example);
    }

    public StoreMarketing add(StoreMarketing record, String createUser) {
        record.setDeleted(StoreMarketing.NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
        return record;
    }

    public StoreMarketing edit(StoreMarketing record, String updateUser) {
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, StoreMarketing.Column.excludes(StoreMarketing.Column.storeId, StoreMarketing.Column.deleted,
                StoreMarketing.Column.createUser, StoreMarketing.Column.createTime));
        return record;
    }

    public void delete(StoreMarketing record, String updateUser) {
        record.setDeleted(StoreMarketing.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, StoreMarketing.Column.deleted, StoreMarketing.Column.updateUser, StoreMarketing.Column.updateTime);
    }
}